package com.sgb.kjwl.view.ui.adapter.pick;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.pick.PreNoticeItemEntity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.DateUtils;

import java.util.List;

public class PickAllStatueListAdapter extends BaseRecycleAdapter<PreNoticeItemEntity> {

    private PickHomeHotChildHeadAdapter pickHomeHotChildHeadAdapter;
    private RecyclerView recyclerView;
    private LinearLayout ll_gc;
    private LinearLayout ll_goods;
    private TextView name1;
    private LinearLayout noList;
    private int tabPosition = 0;
    private IChildClick iChildClick;

    public PickAllStatueListAdapter(Context context, List<PreNoticeItemEntity> datas) {
        super(context, R.layout.item_pick_notice, datas);
    }


    @Override
    public void convert(ItemViewHolder holder, PreNoticeItemEntity goodsInfoEntity3) {
        holder.setBinding(BR.viewModel, goodsInfoEntity3);

        recyclerView = holder.itemView.findViewById(R.id.child_rv);
        ll_gc = holder.itemView.findViewById(R.id.ll_gc);
        ll_goods = holder.itemView.findViewById(R.id.ll_goods);
        name1 = holder.itemView.findViewById(R.id.tv_name1);
        noList = holder.itemView.findViewById(R.id.no_list);

        if (goodsInfoEntity3.getGoodsHeaders() == null || goodsInfoEntity3.getGoodsLists() == null) {
            recyclerView.setVisibility(View.GONE);
            noList.setVisibility(View.VISIBLE);
        } else {
            recyclerView.setVisibility(View.VISIBLE);
            noList.setVisibility(View.GONE);
            pickHomeHotChildHeadAdapter = new PickHomeHotChildHeadAdapter(context, goodsInfoEntity3.getGoodsHeaders(), goodsInfoEntity3.getGoodsLists());
            recyclerView.setLayoutManager(new LinearLayoutManager(context));
            recyclerView.setAdapter(pickHomeHotChildHeadAdapter);
        }

        ImageView imgTab = holder.itemView.findViewById(R.id.img_status_bidding);
        TextView tvType3 = holder.itemView.findViewById(R.id.tv_type3);
        TextView tvStartTime = holder.itemView.findViewById(R.id.text_start_time);
        TextView tv_type2 = holder.itemView.findViewById(R.id.tv_type2);
        TextView tvType = holder.itemView.findViewById(R.id.tv_type);

        imgTab.setVisibility(View.VISIBLE);

        // 招标方式：1、资格预审；2、资格后审；3、报价即报名
        if (2 == goodsInfoEntity3.getBusinessType()){   //竞价
            tvType.setVisibility(View.GONE);
            tvStartTime.setVisibility(View.GONE);

            if (2 == goodsInfoEntity3.getState()) {
                imgTab.setBackground(context.getResources().getDrawable(R.mipmap.icn_jjz));
            } else if (3 == goodsInfoEntity3.getState()) {
                imgTab.setBackground(context.getResources().getDrawable(R.mipmap.icn_yjs));
            } else if (5 == goodsInfoEntity3.getState()) {
                imgTab.setBackground(context.getResources().getDrawable(R.drawable.feibiao));
            } else if (7 == goodsInfoEntity3.getState()) {
                imgTab.setBackground(context.getResources().getDrawable(R.drawable.liubiao));
            }
        }else{   //招标

            tvType.setVisibility(View.VISIBLE);
            if (1 == goodsInfoEntity3.getVerticalTypeValue()) {
                tvType.setText("资格预审");
                tvType.setBackgroundResource(R.drawable.bg_purple);
            } else if (2 == goodsInfoEntity3.getVerticalTypeValue()){
                tvType.setText("资格后审");
                tvType.setBackgroundResource(R.drawable.bg_orange);
            }else if (3 == goodsInfoEntity3.getVerticalTypeValue()){
                tvType.setBackgroundResource(R.drawable.bg_price);
                tvType.setText("报名即报价");
            }

            if (goodsInfoEntity3.getState() == 2) {
                imgTab.setBackground(context.getResources().getDrawable(R.mipmap.icn_zbz));
            } else if (goodsInfoEntity3.getState() == 4) {
                imgTab.setBackground(context.getResources().getDrawable(R.mipmap.icn_zbjs));
            } else if (goodsInfoEntity3.getState() == 9) {
                imgTab.setBackground(context.getResources().getDrawable(R.drawable.pick_notice_bmz));
            } else if (goodsInfoEntity3.getState() == 3) {
                imgTab.setBackground(context.getResources().getDrawable(R.mipmap.icon_pick_pbz));
            } else if (goodsInfoEntity3.getState() == 6) {
                imgTab.setBackground(context.getResources().getDrawable(R.mipmap.icn_zbz));
            } else if (goodsInfoEntity3.getState() == 1) {
                imgTab.setBackground(context.getResources().getDrawable(R.mipmap.ic_dzb));
            } else if (goodsInfoEntity3.getState() == 8) {
                imgTab.setBackground(context.getResources().getDrawable(R.drawable.pick_notice_dbm));
            }else {
                imgTab.setVisibility(View.GONE);
            }

            tvStartTime.setVisibility(View.VISIBLE);
            switch (goodsInfoEntity3.getState()) {
                case 2:
                case 6:
                    tvStartTime.setText(String.format("投标结束时间:  " + DateUtils.stampToDay(String.valueOf(goodsInfoEntity3.getWiveTime()))));
                    break;
                case 4:
                    tvStartTime.setText(String.format("定标时间:  " + DateUtils.stampToDay(String.valueOf(goodsInfoEntity3.getWiveTime()))));
                    imgTab.setBackground(context.getResources().getDrawable(R.mipmap.icn_zbjs));
                    break;
                case 9:
                    tvStartTime.setText(String.format("报名结束时间:  " + DateUtils.stampToDay(String.valueOf(goodsInfoEntity3.getWiveTime()))));
                    break;
                case 3:
                    tvStartTime.setText(String.format("开标时间:  " + DateUtils.stampToDay(String.valueOf(goodsInfoEntity3.getWiveTime()))));
                    break;
                case 1:
                    tvStartTime.setText(String.format("投标开始时间:  " + DateUtils.stampToDay(String.valueOf(goodsInfoEntity3.getWiveTime()))));
                    break;
                case 8:
                    tvStartTime.setText(String.format("报名开始时间:  " + DateUtils.stampToDay(String.valueOf(goodsInfoEntity3.getWiveTime()))));
                    break;
            }
        }



        if (TextUtils.isEmpty(goodsInfoEntity3.getTypeName())) {
            tv_type2.setVisibility(View.GONE);
        } else {
            if (tabPosition == 0) {
                tv_type2.setText(goodsInfoEntity3.getTypeName());
                tv_type2.setVisibility(View.VISIBLE);
            } else {
                tv_type2.setVisibility(View.GONE);
            }
        }
        //增加招标方式：公开招标、邀请招标
//        String biddingMethodName = goodsInfoEntity3.getBiddingMethodName();
//        if(!TextUtils.isEmpty(biddingMethodName)){
//            tvType3.setVisibility(View.VISIBLE);
//            if ("公开招标".equals(biddingMethodName)){
//                tvType3.setBackground(context.getResources().getDrawable(R.drawable.bg_green));
//            }else{
//                tvType3.setBackground(context.getResources().getDrawable(R.drawable.bg_orange));
//            }
//            tvType3.setText(goodsInfoEntity3.getBiddingMethodName());
//        }

        pickHomeHotChildHeadAdapter.setOnItemClick(new PickHomeHotChildHeadAdapter.OnItemClick() {
            @Override
            public void onItemClick() {
                if (onItemClick != null){
                    onItemClick.onItemClick(goodsInfoEntity3, holder.getIndex_position());
                }
            }
        });
    }

    public void setTabPosition(int tabPosition){
        this.tabPosition = tabPosition;
        notifyDataSetChanged();
    }

    public void onIChildClick(IChildClick iChildClick) {
        this.iChildClick = iChildClick;
    }

    public interface IChildClick {
        void onClick(PreNoticeItemEntity preNoticeItemEntity);
    }
}
